<?php

class RedisLogger extends dbObject {
	public $user_id;
	public $timestamp;

/*
 * Usage (Contact Logger):
 * 
 * $logger = RedisLogger::ContactLogger($user_id);
 * $logger->log($object_1);
 * $logger->log($object_2);
 * $logger->log($object_n);
 * $logger->commit();
 * 
 * This will log object_1, object_2, and object_n as elements in the log under the single logger key
 *
 * Usage (Calendar Logger):
 * 
 * $logger = RedisLogger::CalendarLogger($user_id, $calendar_id);
 * $logger->log($object_1);
 * $logger->log($object_2);
 * $logger->log($object_n);
 * $logger->commit();
 * 
 * This will log object_1, object_2, and object_n as elements in the log under the single logger key
 *
 */
	
	public static function ContactLogger($user_id) {
		$logger = new RedisLogger($user_id);
		// key = log:contact:[user_id]:[utc_timestamp]
		$logger->dbTable = "log:contact:{$user_id}:{$logger->timestamp}";
		return $logger;
	}

	public static function CalendarLogger($user_id, $calendar_id) {
		$logger = new RedisLogger($user_id);
		// key = log:contact:[user_id]:[calendar]:[utc_timestamp]
		$logger->dbTable = "log:calendar:{$user_id}:{$calendar_id}:{$logger->timestamp}";
		return $logger;
	}

	private function __construct($user_id) {
		$this->user_id = $user_id;
		$this->timestamp = date('Y-m-d\TH:i:s');
		$this->tableKey = 'user_id';
		$this->keyType = 'list';
		
		$this->members = array();
		$this->expire = 1209600; // two weeks
	}
	
	// takes either a single item or an array of items to log
	// items can either be an object, which will be json encoded, or an already json-encoded string
	public function log($entry) {
		if (is_array($entry)) {
			foreach ($entry as $e) {
				if (!is_string($e)) $e = json_encode($e);
				$this->members[] = $e;
			}
		} else {
			if (!is_string($entry)) $entry = json_encode($entry);
			$this->members[] = $entry;
		}
	}
	
	public function commit() {
		$this->insert();
	}
}

?>